package com.sunchangpeng.wechat.mp.stat;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;
import com.sunchangpeng.wechat.mp.EndpointProvider;
import com.sunchangpeng.wechat.mp.MpAppSetting;
import com.sunchangpeng.wechat.mp.MpWechatClientFactory;
import lombok.Data;

import java.time.LocalDate;
import java.util.List;

public class ArticleStats extends Stats {
    public static ArticleStats with(MpAppSetting appSetting) {
        ArticleStats stats = new ArticleStats();
        stats.setWechatClient(MpWechatClientFactory.getInstance().with(appSetting));
        return stats;
    }

    /**
     * 获取图文群发每日数据
     */

    public List<ArticleSummary> getArticleSummary(LocalDate day) {
        String url = EndpointProvider.get("url.stats.article.summary");
        return JSON.parseObject(doRequest(url, day, day), ArticleSummaryWrapper.class).getList();
    }

    /**
     * 获取图文群发总数据
     */
    public List<ArticleTotal> getArticleTotal(LocalDate day) {
        String url = EndpointProvider.get("url.stats.article.total");
        return JSON.parseObject(doRequest(url, day, day), ArticleTotalWrapper.class).getList();
    }

    /**
     * 获取图文统计数据
     */
    public List<ArticleSummary> getUserRead(LocalDate start, LocalDate end) {
        if (diffThanDays(start, end, 3)) {
            throw new IllegalArgumentException("start和end相差不能超过3天以上");
        }

        String url = EndpointProvider.get("url.stats.article.userread");
        return JSON.parseObject(doRequest(url, start, end), ArticleSummaryWrapper.class).getList();
    }

    /**
     * 获取图文统计分时数据
     */
    public List<ArticleSummary> getUserReadHour(LocalDate day) {
        String url = EndpointProvider.get("url.stats.article.userreadhour");
        return JSON.parseObject(doRequest(url, day, day), ArticleSummaryWrapper.class).getList();
    }

    /**
     * 获取图文分享转发数据
     */
    public List<ArticleSummary> getUserShare(LocalDate start, LocalDate end) {
        if (diffThanDays(start, end, 7)) {
            throw new IllegalArgumentException("start和end相差不能超过7天以上");
        }

        String url = EndpointProvider.get("url.stats.article.usershare");
        return JSON.parseObject(doRequest(url, start, end), ArticleSummaryWrapper.class).getList();
    }

    /**
     * 获取图文分享转发分时数据
     */
    public List<ArticleSummary> getUserShareHour(LocalDate day) {
        String url = EndpointProvider.get("url.stats.article.usersharehour");
        return JSON.parseObject(doRequest(url, day, day), ArticleSummaryWrapper.class).getList();
    }

    @Data
    public static class ArticleSummary {
        @JSONField(name = "ref_date")
        private String refDate;

        @JSONField(name = "ref_hour")
        private Integer refHour;

        @JSONField(name = "msgid")
        private String msgId;

        private String title;

        @JSONField(name = "int_page_read_user")
        private Integer intPageReadUser;

        @JSONField(name = "int_page_read_count")
        private Integer intPageReadCount;

        @JSONField(name = "ori_page_read_user")
        private Integer oriPageReadUser;

        @JSONField(name = "ori_page_read_count")
        private Integer oriPageReadCount;

        @JSONField(name = "share_scene")
        private Integer shareScene;

        @JSONField(name = "share_user")
        private Integer shareUser;

        @JSONField(name = "share_count")
        private Integer shareCount;

        @JSONField(name = "add_to_fav_user")
        private Integer addToFavUser;

        @JSONField(name = "add_to_fav_count")
        private Integer addToFavCount;

        @JSONField(name = "user_source")
        private Integer userSource;
    }

    @Data
    public static class ArticleSummaryWrapper {
        private List<ArticleSummary> list;
    }


    @Data
    public static class ArticleTotalWrapper {
        private List<ArticleTotal> list;
    }

    @Data
    public static class ArticleTotal {
        @JSONField(name = "ref_date")
        private String refDate;

        @JSONField(name = "msgid")
        private String msgId;

        @JSONField(name = "title")
        private String title;

        @JSONField(name = "details")
        private List<ArticleTotalDetail> details;
    }


    @Data
    public static class ArticleTotalDetail {
        @JSONField(name = "stat_date")
        private String statDate;

        @JSONField(name = "target_user")
        private Integer targetUser;

        @JSONField(name = "int_page_read_user")
        private Integer intPageReadUser;

        @JSONField(name = "int_page_read_count")
        private Integer intPageReadCount;

        @JSONField(name = "ori_page_read_user")
        private Integer oriPageReadUser;

        @JSONField(name = "ori_page_read_count")
        private Integer oriPageReadCount;

        @JSONField(name = "share_user")
        private Integer shareUser;

        @JSONField(name = "share_count")
        private Integer shareCount;

        @JSONField(name = "add_to_fav_user")
        private Integer addToFavUser;

        @JSONField(name = "add_to_fav_count")
        private Integer addToFavCount;

        @JSONField(name = "int_page_from_session_read_user")
        private Integer intPageFromSessionReadUser;

        @JSONField(name = "int_page_from_session_read_count")
        private Integer intPageFromSessionReadCount;

        @JSONField(name = "int_page_from_hist_msg_read_user")
        private Integer intPageFromHistMsgReadUser;

        @JSONField(name = "int_page_from_hist_msg_read_count")
        private Integer intPageFromHistMsgReadCount;

        @JSONField(name = "int_page_from_feed_read_user")
        private Integer intPageFromFeedReadUser;

        @JSONField(name = "int_page_from_feed_read_count")
        private Integer intPageFromFeedReadCount;

        @JSONField(name = "int_page_from_friends_read_user")
        private Integer intPageFromFriendsReadUser;

        @JSONField(name = "int_page_from_friends_read_count")
        private Integer intPageFromFriendsReadCount;

        @JSONField(name = "int_page_from_other_read_user")
        private Integer intPageFromOtherReadUser;

        @JSONField(name = "int_page_from_other_read_count")
        private Integer intPageFromOtherReadCount;

        @JSONField(name = "feed_share_from_session_user")
        private Integer feedShareFromSessionUser;

        @JSONField(name = "feed_share_from_session_cnt")
        private Integer feedShareFromSessionCnt;

        @JSONField(name = "feed_share_from_feed_user")
        private Integer feedShareFromFeedUser;

        @JSONField(name = "feed_share_from_feed_cnt")
        private Integer feedShareFromFeedCnt;

        @JSONField(name = "feed_share_from_other_user")
        private Integer feedShareFromOtherUser;

        @JSONField(name = "feed_share_from_other_cnt")
        private Integer feedShareFromOtherCnt;
    }
}
